﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace OMSService
{
    public class ItemManager
    {
        #region CRUD

        public static ItemModel New(string name, string description, decimal price, int quantity)
        {
            ItemModel itemModel = new ItemModel(name, description, price, quantity);
            return itemModel;
        }

        public static bool Save(ItemModel itemModel)
        {
            //TODO:Add error handling
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    Item item = new Item
                    {
                        Name = itemModel.Name,
                        Description = itemModel.Description,
                        Price = itemModel.Price,
                        Quantity = itemModel.Quantity
                    };
                    context.Items.AddObject(item);
                    context.SaveChanges();
                }
            }
            catch (DataException ex)
            {
                return false;
            }
            return true;
        }

        public static bool Create(string name, string description, decimal price, int quantity)
        {
            ItemModel itemModel = New(name, description, price, quantity);
            return (Save(itemModel));
        }

        public static bool Update(ItemModel itemModel)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    Item item = (from m in context.Items where m.Id == itemModel.Id select m).First();
                    item.Name = itemModel.Name;
                    item.Description = itemModel.Description;
                    item.Price = itemModel.Price;
                    item.Quantity = itemModel.Quantity;
                    context.Items.ApplyCurrentValues(item);
                    context.SaveChanges();
                }
            }
            catch (DataException ex)
            {
                return false;
            }
            return true;
        }

        public static bool Delete(int id)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    Item item = (from m in context.Items where m.Id == id select m).First();
                    context.Items.DeleteObject(item);
                    context.SaveChanges();
                }
            }
            catch (DataException ex)
            {
                return false;
            }
            return true;
        }

        #region Get

        public static ItemModel GetById(int id)
        {
            Item item;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                item = (from m in context.Items where m.Id == id select m).First();
            }
            ItemModel itemModel = new ItemModel(item);
            return itemModel;
        }

        public static List<ItemModel> GetAll()
        {
            List<Item> items;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                items = context.Items.ToList();
            }
            List<ItemModel> tmpItems = new List<ItemModel>();
            foreach (Item i in items)
            {
                tmpItems.Add(new ItemModel(i));
            }
            return tmpItems;
        }

        #endregion
        #endregion

        #region Filters

        public static List<ItemModel> FilterByName(string itemName)
        //TODO: test
        {
            List<ItemModel> itemModels = new List<ItemModel>();
            List<Item> items;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                items = context.Items.Where(s => s.Name.ToLower().StartsWith(itemName.ToLower())).ToList();
            }
            foreach(Item item in items)
            {
                itemModels.Add(new ItemModel(item));
            }
            return itemModels;
        }

        public static List<ItemModel> FilterByDescription(string itemDescription)
        //TODO: test
        {
            List<ItemModel> itemModels = new List<ItemModel>();
            List<Item> items;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                items = context.Items.Where(s => s.Description.ToLower().StartsWith(itemDescription.ToLower())).ToList();
            }
            foreach (Item item in items)
            {
                itemModels.Add(new ItemModel(item));
            }
            return itemModels;
        }

        #endregion
    }
}